//
//  ZYCLoginView.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/17.
//

#import "ZYCLoginView.h"

@interface ZYCLoginView ()

@property (nonatomic, strong) UIImageView *topRightImageView;
@property (nonatomic, strong) UIImageView *bottomImageView;
@property (nonatomic, strong) UILabel *weclomeLabel;
@property (nonatomic, strong) UILabel *schoolNameLabel;
@property (nonatomic, strong) UIView *userNameBgView;
@property (nonatomic, strong) UIView *passwordBgView;
@property (nonatomic, strong) UIImageView *userNameImgView;
@property (nonatomic, strong) UIImageView *passwordImgView;
@property (nonatomic, strong) UITextField *userNameTextField;
@property (nonatomic, strong) UITextField *passTextField;
@property (nonatomic, strong) UIButton *loginButton;

@end
@implementation ZYCLoginView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.topRightImageView];
        [self addSubview:self.bottomImageView];
        [self addSubview:self.weclomeLabel];
        [self addSubview:self.schoolNameLabel];
        [self addSubview:self.userNameBgView];
        [self addSubview:self.passwordBgView];
        [self addSubview:self.testLabel];
        [self.userNameBgView addSubview:self.userNameImgView];
        [self.userNameBgView addSubview:self.userNameTextField];
        [self.passwordBgView addSubview:self.passwordImgView];
        [self.passwordBgView addSubview:self.passTextField];
        [self addSubview:self.loginButton];
        [self setupConstraints];
    }
    return self;
}

- (UITextField *)userNameTextField {
    if (!_userNameTextField) {
        _userNameTextField = [[UITextField alloc] init];
        _userNameTextField.placeholder = @"请输入用户名";
        _userNameTextField.font = kCustomFont(@"PingFangSC-Regular", 14.0f);
        _userNameTextField.textColor = HEX_RGB(kTitleThemeColor);
        _userNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _userNameTextField.keyboardType = UIKeyboardTypeNumberPad;
    }
    return _userNameTextField;
}

- (UITextField *)passTextField {
    if (!_passTextField) {
        _passTextField = [[UITextField alloc] init];
        _passTextField.placeholder = @"请输入密码";
        _passTextField.font = kCustomFont(@"PingFangSC-Regular", 14.0f);
        _passTextField.textColor = HEX_RGB(kTitleThemeColor);
        _passTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _passTextField.secureTextEntry = YES;
    }
    return _passTextField;
}

- (UIButton *)loginButton {
    if (!_loginButton) {
        _loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _loginButton.frame = CGRectMake(0,0, kScreenWidth - 2 * 32, 48);
        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
        [_loginButton setTitleColor:HEX_RGB(kTitleWhiteColor) forState:UIControlStateNormal];
        _loginButton.backgroundColor = HEX_RGB(@"#39A85B");
        _loginButton.titleLabel.font = kSystemBoldFont(22.0f);
        [_loginButton addTarget:self action:@selector(clickLoginAction:) forControlEvents:UIControlEventTouchUpInside];
       _loginButton.layer.cornerRadius = 24;
        _loginButton.layer.masksToBounds = YES;
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(1, 0);
        gradientLayer.colors = @[(__bridge id)HEX_RGB(@"#52E1E3").CGColor,
                                 (__bridge id)HEX_RGB(@"#8BDFBC").CGColor];
        gradientLayer.locations = @[@(0.3),@(1.0f)];
        gradientLayer.frame = _loginButton.bounds;
        gradientLayer.cornerRadius = 24;
        [_loginButton.layer insertSublayer:gradientLayer atIndex:0];
    }
    return _loginButton;
}

- (UIImageView *)userNameImgView {
    if (!_userNameImgView) {
        _userNameImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yhm"]];
    }
    return _userNameImgView;
}

- (UIImageView *)passwordImgView {
    if (!_passwordImgView) {
        _passwordImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mm"]];
    }
    return _passwordImgView;
}

- (UIView *)userNameBgView {
    if (!_userNameBgView) {
        _userNameBgView = [[UIView alloc] init];
        _userNameBgView.backgroundColor = HEX_RGB(kBgGrayColor);
        _userNameBgView.layer.cornerRadius = 41.6 / 2;
        _userNameBgView.layer.masksToBounds = YES;
    }
    return _userNameBgView;
}

- (UIView *)passwordBgView {
    if (!_passwordBgView) {
        _passwordBgView = [[UIView alloc] init];
        _passwordBgView.backgroundColor = HEX_RGB(kBgGrayColor);
        _passwordBgView.layer.cornerRadius = 41.6 / 2;
        _passwordBgView.layer.masksToBounds = YES;
    }
    return _passwordBgView;
}

- (UILabel *)schoolNameLabel {
    if (!_schoolNameLabel) {
        _schoolNameLabel = [UILabel LabelWithString:@"湖南师大附中星沙试验学校" Font:[UIFont systemFontOfSize:17.0f weight:UIFontWeightRegular] titleColor:HEX_RGB(kTitleMainColor) textAlignment:ZZTextAlignmentLeft];
    }
    return _schoolNameLabel;
}

- (UILabel *)weclomeLabel {
    if (!_weclomeLabel) {
        _weclomeLabel = [UILabel LabelWithString:@"欢迎登录" Font:kSystemBoldFont(27.0f) titleColor:HEX_RGB(kTitleDrakBlackColor) textAlignment:ZZTextAlignmentLeft];
    }
    return _weclomeLabel;
}
- (UIImageView *)topRightImageView {
    if (!_topRightImageView) {
        _topRightImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bja"]];
    }
    return _topRightImageView;
}

- (UIImageView *)bottomImageView {
    if (!_bottomImageView) {
        _bottomImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bjb"]];
    }
    return _bottomImageView;
}

- (UILabel *)testLabel {
    if (!_testLabel) {
        _testLabel = [UILabel LabelWithFont:kCustomFont(@"PingFangSC-Light", 14.0f) titleColor:HEX_RGB(@"#262A28") textAlignment:ZZTextAlignmentLeft];
        _testLabel.text = @"测试环境";
        _testLabel.hidden = YES;
    }
    return _testLabel;
}

-(NSString *)userName{
    return self.userNameTextField.text;
}

-(NSString *)password{
    return self.passTextField.text;
}

- (void)clickLoginAction:(UIButton *)sender{
    if ([self.delegate respondsToSelector:@selector(loginViewDidLogin)]) {
        [self.delegate loginViewDidLogin];
    }
}


- (void)setupConstraints{
    [self.topRightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(142, 142));
    }];
    
    [self.bottomImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(103);
    }];
    
    [self.testLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self).offset(50.0f);
    }];
    
    [self.weclomeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(32.0f);
        make.top.equalTo(self).offset(55.0f + kNavBarAndStatusBarHeight);
    }];
    
    [self.schoolNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.weclomeLabel);
        make.top.equalTo(self.weclomeLabel.mas_bottom).offset(15.0f);
    }];
    
    [self.userNameBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(32.0f);
        make.right.equalTo(self).offset(-32.0f);
        make.top.equalTo(self.schoolNameLabel.mas_bottom).offset(85.0f);
        make.height.mas_equalTo(45.0f);
    }];
    
    [self.passwordBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(32.0f);
        make.right.equalTo(self).offset(-32.0f);
        make.top.equalTo(self.userNameBgView.mas_bottom).offset(15.0f);
        make.height.mas_equalTo(45.0f);
    }];
    
    [self.userNameImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.userNameBgView).offset(30.0f);
        make.centerY.equalTo(self.userNameBgView);
        make.size.mas_equalTo(CGSizeMake(17.0, 17.0f));
    }];
    
    [self.passwordImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.passwordBgView).offset(30.0f);
        make.centerY.equalTo(self.passwordBgView);
        make.size.mas_equalTo(CGSizeMake(17.0, 17.0f));
    }];
    
    [self.userNameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.userNameImgView.mas_right).offset(10.0f);
        make.right.equalTo(self.userNameBgView);
        make.centerY.equalTo(self.userNameBgView);
    }];
    
    [self.passTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.passwordImgView.mas_right).offset(10.0f);
        make.right.equalTo(self.passwordBgView);
        make.centerY.equalTo(self.passwordBgView);
    }];
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(32.0f);
        make.right.equalTo(self).offset(-32.0f);
        make.top.equalTo(self.passwordBgView.mas_bottom).offset(37.0f);
        make.height.mas_equalTo(48.0f);
    }];
}



@end
